<!--学生查看活动列表-->
<link rel="stylesheet" href="./css/studentActivity.css">

<div id="big">

</div>

<nav aria-label="...">
	<ul class="pager">
		<li>
			<a href="#" id="pre">上一页</a>
		</li>
		<li>
			<a href="#" id="top">首页</a>
		</li>
		<li>第<span class="num">1</span>页</li>
		<li>
			<a href="#" id="end">尾页</a>
		</li>
		<li>
			<a href="#" id="next">下一页</a>
		</li>
	</ul>
</nav>

<script>
	function fenye(data) {
		if(data.status == 0) {
			$.session.set("prePageNum", data.page.prePageNum);
			$.session.set("nextPageNum", data.page.nextPageNum);
			$.session.set("totalPageNum", data.page.totalPageNum);
			$('.num').html(data.page.pageNum);
			$('#big').html("");

			var list = data.page.list;
			for(var i = 0; i < list.length; i++) {
				var str = `
		          	<div class="tongzhi" id="look_` + list[i].aid + `">
					    <h3>` + list[i].title + `</h3>
					    <div class="content" id="look" index="lookactivity">` + list[i].context + `</div>
					    <div class="footer">` + timeStamp2String(list[i].createTime) + `</div>
					    <input type="hidden" id="hidden" value="` + list[i].aid + `"> 
					</div>
		        `;

				var div = $('<div></div>');
				div.html(str);
				$('#big').append(div);

				var look = '#look_' + list[i].aid;

				$(look).click(function() {
					$.session.set('aid', $(this).children("input").eq(0).val());
					$('.center').html('');
					$('.center').load('./lookactivity.html');
				})

				//for结尾
			}
		}
	}

	$.ajax({
		xhrFields: {
			withCredentials: true
		},
		type: "GET",
		url: getFullURL("all-activity-list"),
		data: {
			pageNum: 1,
			pageSize: 10,
			type: 0
		},
		success: function(data) {
			console.log(data);
			fenye(data);
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	})

	$("#top").click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			url: getFullURL("all-activity-list"),
			data: {
				pageNum: 1,
				pageSize: 10,
				type: 0
			},
			success: function(data) {
				fenye(data);
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	$("#end").click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			url: getFullURL("all-activity-list"),
			data: {
				pageNum: $.session.get("totalPageNum"),
				pageSize: 10,
				type: 0
			},
			success: function(data) {
				fenye(data);
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	$("#pre").click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			url: getFullURL("all-activity-list"),
			data: {
				pageNum: $.session.get("prePageNum"),
				pageSize: 10,
				type: 0
			},
			success: function(data) {
				fenye(data);
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	$("#next").click(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			url: getFullURL("all-activity-list"),
			data: {
				pageNum: $.session.get("nextPageNum"),
				pageSize: 10,
				type: 0
			},
			success: function(data) {
				fenye(data);
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	function timeStamp2String(time) {
		var datetime = new Date();
		datetime.setTime(time);
		var year = datetime.getFullYear();
		var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	}
</script>